package com.y.test;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//�����
public class Server1 {
	
	private ServerSocket server;
	private Map map;
	
	//���췽��
 	public Server1()throws Exception{
		 server = new ServerSocket(8081);
		 map = new HashMap();
	}
	
 	private synchronized void addOut(String string,PrintWriter out){
 		map.put(string, out);
 	}
 	private synchronized void removeOut(String string){
 		map.remove(string);
 	}
 	
 	

 	private synchronized void sendMessage(String message,String message2){
 		PrintWriter pw = null;
 		Object a =map.get(message);
 		if (a instanceof PrintWriter) {
 			pw = (PrintWriter)a;
		}
 		pw.println(message2);
 	}

 	
 	
	//��ʼ����
	public void start(){
		Scanner s = new Scanner(System.in);
		try {
			/*
			 * SeverSocket��accept������һ��
			 * ���������������Ǽ�������˿ڣ�֪��һ���ͻ������Ӳ�����һ��
			 * Socket��ʹ�ø�Socket
			 * ����������ӵĿͻ��˽��н���
			 */
			while (true) {
				System.out.println("�ȴ��ͻ������ӡ�����������");
				Socket socket = server.accept();
				System.out.println("һ���ͻ��������ˣ�");
				ClientHandler handler = new ClientHandler(socket);
				Thread t = new Thread(handler);
				t.start();
			}

			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//������
	public static void main(String[] args) {
		try {
			Server1 server = new Server1();
			server.start();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("���������ʧ�ܣ�");
		}
	}
	
	//�߳�����
	class ClientHandler implements Runnable{
		private Socket socket;
		private String host;	
		private String nickName;
		
		public ClientHandler (Socket socket){
			this.socket = socket;
			InetAddress address = socket.getInetAddress();
			host = address.getHostAddress();
		}
		public void run() {
			PrintWriter pw = null;
			
			try {
				
				InputStream in = socket.getInputStream();
				InputStreamReader isr = new InputStreamReader(in,"UTF-8");
				BufferedReader br = new BufferedReader(isr);
				
				nickName = br.readLine();
				System.out.println(nickName+"������");
				
				OutputStream out = socket.getOutputStream();
				OutputStreamWriter osw = new OutputStreamWriter(out,"UTF-8");
				pw = new PrintWriter(osw,true);
				
				addOut(nickName,pw);
				String message = null;
				while ((message = br.readLine())!=null) {
					if (message.startsWith("@")) {
						String jiequ = message.substring(1, 4);
						sendMessage(jiequ,message);
						
					}else {
						Set<Entry<String,PrintWriter>>entrySet = map.entrySet();
						for (Entry<String,PrintWriter> e : entrySet) {
							PrintWriter p = e.getValue();
							p.println(nickName+"˵��"+message);
						}
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				try {
					socket.close();
					removeOut(nickName);
				} catch (IOException e) {
					e.printStackTrace();
				}
				System.out.println(nickName+"�����ˣ�");
			}
		}		
	}
}


